function varargout = UI1(varargin)
% UI1 MATLAB code for UI1.fig
%	  UI1, by itself, creates a new UI1 or raises the existing
%	  singleton*.
%
%	  H = UI1 returns the handle to a new UI1 or the handle to
%	  the existing singleton*.
%
%	  UI1('CALLBACK',hObject,eventData,handles,...) calls the local
%	  function named CALLBACK in UI1.M with the given input arguments.
%
%	  UI1('Property','Value',...) creates a new UI1 or raises the
%	  existing singleton*.  Starting from the left, property value pairs are
%	  applied to the GUI before UI1_OpeningFcn gets called.  An
%	  unrecognized property name or invalid value makes property application
%	  stop.  All inputs are passed to UI1_OpeningFcn via varargin.
%
%	  *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%	  instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help UI1

% Last Modified by GUIDE v2.5 10-Sep-2014 12:13:55

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',	   mfilename, ...
				   'gui_Singleton',  gui_Singleton, ...
				   'gui_OpeningFcn', @UI1_OpeningFcn, ...
				   'gui_OutputFcn',  @UI1_OutputFcn, ...
				   'gui_LayoutFcn',  [] , ...
				   'gui_Callback',   []);
if nargin && ischar(varargin{1})
	gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
	[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
	gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before UI1 is made visible.
function UI1_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject	handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles	structure with handles and user data (see GUIDATA)
% varargin   command line arguments to UI1 (see VARARGIN)

% Choose default command line output for UI1
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes UI1 wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = UI1_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject	handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles	structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject	handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles	structure with handles and user data (see GUIDATA)
global len; 
global id;
global filenamestring;
if len <= 0
	msgbox('please choose a folder first!');
	return
end
id = 1;
info_display(handles, filenamestring{id,2});

% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject	handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles	structure with handles and user data (see GUIDATA)
global len; 
global id;
global filenamestring;
if len <= 0
	msgbox('please choose a folder first!');
	return
end
if id > 1
	id = id-1;
else
	id = len;
end
info_display(handles, filenamestring{id,2});

% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject	handle to pushbutton3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles	structure with handles and user data (see GUIDATA)
global len; 
global id;
global filenamestring;
if len <= 0
	msgbox('please choose a folder first!');
	return
end
if id < len
	id = id+1;
else
	id = 1;
end
info_display(handles, filenamestring{id,2});

% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject	handle to pushbutton4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles	structure with handles and user data (see GUIDATA)
global len; 
global id;
global filenamestring;
if len <= 0
	msgbox('please choose a folder first!');
	return
end
id = len;
info_display(handles, filenamestring{id,2});

% --- Executes during object creation, after setting all properties.
function figure1_CreateFcn(hObject, eventdata, handles)
% hObject	handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles	empty - handles not created until after all CreateFcns called
global len;
len = -1;


% --- Executes on button press in pushbutton6.
function pushbutton6_Callback(hObject, eventdata, handles)
% hObject	handle to pushbutton6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles	structure with handles and user data (see GUIDATA)
global len;
global id;
global filepath;
global filenamestring;
filepath = uigetdir(cd, 'Choose a folder');
temp = [filepath, '/*.dcm'];
DICOM_files = ls(temp);

if isempty(DICOM_files)
	msgbox('There are no DICOM files!');
	return;
end

filenametemp = cellstr(DICOM_files);
len = length(filenametemp);
tempcell = cell(len, 2);
for i = 1 : len
	temp = [filepath, '\', filenametemp{i}];

	temp_info = dicominfo(temp);
	tempcell{i,1} = temp_info.InstanceNumber;
	tempcell{i,2} = temp;
end

filenamestring = sortrows(tempcell);


if len > 0 
	id = 1;
	info_display(handles, filenamestring{id,2});
end


% --- Executes on button press in pushbutton8.
function pushbutton8_Callback(hObject, eventdata, handles)
% hObject	handle to pushbutton8 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles	structure with handles and user data (see GUIDATA)
global len;
global filepath;
global filenamestring;

if len <= 0
	msgbox('please choose a folder first!');
	return
end

info = dicominfo(filenamestring{1,2});

%%%%%have question%%%%%%%
width = info.Width;
%width = info.Columns;
height = info.Height;
%height = info.Rows;
interval = info.SpacingBetweenSlices;
%interval = info.SliceThickness;
depth = len * interval;

original = zeros(width, height, len);
voxel = zeros(width, height, depth);
tempimg1 = zeros(len, height);


for k = 1 : len
	info = dicominfo(filenamestring{k,2});
	tempimg = dicomread(info);
	
	for i = 1 : width
		for j = 1 : height
			original(i,j,k) = tempimg(i,j);
		end
	end 
	
end

for i = 1: width
	for j = 1 : height
		for k = 1 : len
			tempimg1(len - k + 1,j) = original(i,j,k);
		end
	end

	tempimg2 = imresize(tempimg1, [depth, height]);
	
	for j = 1 : height
		for k = 1 : depth
			voxel(i,j,k) = tempimg2(k,j);
		end
	end	 
	axes(handles.axes1);
	imshow(tempimg2, []);
end

return;


% --- Executes on button press in pushbutton9.
function pushbutton9_Callback(hObject, eventdata, handles)
% hObject	handle to pushbutton9 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles	structure with handles and user data (see GUIDATA)
global len;
global id;
global filenamestring;

if len <= 0
	msgbox('please choose a folder first!');
	return
end

info = dicominfo(filenamestring{id,2});
img = dicomread(info);
h = ones(3,3)/9;
temp = imfilter(img,h);
%temp = img;
t2 = mean2(temp);
BW = ((temp) >= t2);
BW2 = bwmorph(BW, 'clean');
axes(handles.axes1);
imshow(BW2, []);

   